﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Hermes.DAL
{
	/// <summary>
	/// 数据访问类:Address
	/// </summary>
	public partial class Address
	{
		public Address()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Hermes.Model.Address model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Address(");
			strSql.Append("Contact,Company,Street,District,City,Province,Postcode,Country,Email,Mobile,Phone,LastUpdate,Type)");
			strSql.Append(" values (");
			strSql.Append("@Contact,@Company,@Street,@District,@City,@Province,@Postcode,@Country,@Email,@Mobile,@Phone,@LastUpdate,@Type)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Contact", SqlDbType.NVarChar,200),
					new SqlParameter("@Company", SqlDbType.NVarChar,200),
					new SqlParameter("@Street", SqlDbType.NVarChar,500),
					new SqlParameter("@District", SqlDbType.NVarChar,50),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.NVarChar,50),
					new SqlParameter("@Postcode", SqlDbType.NVarChar,50),
					new SqlParameter("@Country", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@LastUpdate", SqlDbType.Timestamp,8),
					new SqlParameter("@Type", SqlDbType.Int,4)};
			parameters[0].Value = model.Contact;
			parameters[1].Value = model.Company;
			parameters[2].Value = model.Street;
			parameters[3].Value = model.District;
			parameters[4].Value = model.City;
			parameters[5].Value = model.Province;
			parameters[6].Value = model.Postcode;
			parameters[7].Value = model.Country;
			parameters[8].Value = model.Email;
			parameters[9].Value = model.Mobile;
			parameters[10].Value = model.Phone;
			parameters[11].Value = model.LastUpdate;
			parameters[12].Value = model.Type;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Hermes.Model.Address model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Address set ");
			strSql.Append("Contact=@Contact,");
			strSql.Append("Company=@Company,");
			strSql.Append("Street=@Street,");
			strSql.Append("District=@District,");
			strSql.Append("City=@City,");
			strSql.Append("Province=@Province,");
			strSql.Append("Postcode=@Postcode,");
			strSql.Append("Country=@Country,");
			strSql.Append("Email=@Email,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("Type=@Type");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@Contact", SqlDbType.NVarChar,200),
					new SqlParameter("@Company", SqlDbType.NVarChar,200),
					new SqlParameter("@Street", SqlDbType.NVarChar,500),
					new SqlParameter("@District", SqlDbType.NVarChar,50),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.NVarChar,50),
					new SqlParameter("@Postcode", SqlDbType.NVarChar,50),
					new SqlParameter("@Country", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.Contact;
			parameters[1].Value = model.Company;
			parameters[2].Value = model.Street;
			parameters[3].Value = model.District;
			parameters[4].Value = model.City;
			parameters[5].Value = model.Province;
			parameters[6].Value = model.Postcode;
			parameters[7].Value = model.Country;
			parameters[8].Value = model.Email;
			parameters[9].Value = model.Mobile;
			parameters[10].Value = model.Phone;
			parameters[11].Value = model.Type;
			parameters[12].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Address ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Address ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Hermes.Model.Address GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,Contact,Company,Street,District,City,Province,Postcode,Country,Email,Mobile,Phone,LastUpdate,Type from Address ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			Hermes.Model.Address model=new Hermes.Model.Address();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Contact"]!=null && ds.Tables[0].Rows[0]["Contact"].ToString()!="")
				{
					model.Contact=ds.Tables[0].Rows[0]["Contact"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Company"]!=null && ds.Tables[0].Rows[0]["Company"].ToString()!="")
				{
					model.Company=ds.Tables[0].Rows[0]["Company"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Street"]!=null && ds.Tables[0].Rows[0]["Street"].ToString()!="")
				{
					model.Street=ds.Tables[0].Rows[0]["Street"].ToString();
				}
				if(ds.Tables[0].Rows[0]["District"]!=null && ds.Tables[0].Rows[0]["District"].ToString()!="")
				{
					model.District=ds.Tables[0].Rows[0]["District"].ToString();
				}
				if(ds.Tables[0].Rows[0]["City"]!=null && ds.Tables[0].Rows[0]["City"].ToString()!="")
				{
					model.City=ds.Tables[0].Rows[0]["City"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Province"]!=null && ds.Tables[0].Rows[0]["Province"].ToString()!="")
				{
					model.Province=ds.Tables[0].Rows[0]["Province"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Postcode"]!=null && ds.Tables[0].Rows[0]["Postcode"].ToString()!="")
				{
					model.Postcode=ds.Tables[0].Rows[0]["Postcode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Country"]!=null && ds.Tables[0].Rows[0]["Country"].ToString()!="")
				{
					model.Country=ds.Tables[0].Rows[0]["Country"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Mobile"]!=null && ds.Tables[0].Rows[0]["Mobile"].ToString()!="")
				{
					model.Mobile=ds.Tables[0].Rows[0]["Mobile"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Phone"]!=null && ds.Tables[0].Rows[0]["Phone"].ToString()!="")
				{
					model.Phone=ds.Tables[0].Rows[0]["Phone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LastUpdate"]!=null && ds.Tables[0].Rows[0]["LastUpdate"].ToString()!="")
				{
					model.LastUpdate=DateTime.Parse(ds.Tables[0].Rows[0]["LastUpdate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Type"]!=null && ds.Tables[0].Rows[0]["Type"].ToString()!="")
				{
					model.Type=int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,Contact,Company,Street,District,City,Province,Postcode,Country,Email,Mobile,Phone,LastUpdate,Type ");
			strSql.Append(" FROM Address ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,Contact,Company,Street,District,City,Province,Postcode,Country,Email,Mobile,Phone,LastUpdate,Type ");
			strSql.Append(" FROM Address ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Address";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

